<template>
  <div class="inventoryPosting">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.fdate') }}</span>
        <el-date-picker
          v-model="fdate"
          type="month"
          value-format="yyyy-MM"
          size="mini"
          style="width: 152px"
        />
        <span>{{ $t('market.fmaterial') }}</span><el-input v-model="workShopName" size="mini" style="width: 180px" />
        <span>{{ $t('market.stock2') }}</span>
        <el-input v-model="stockname" size="mini" style="width: 180px">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="seleceVisiblit=true"
          />
        </el-input>
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <!-- 仓库 -->
    <selectCK v-if="seleceVisiblit" :msg="1" @client="selectCK" />
  </div>
</template>

<script>
import {
  queryRevenueAndExpenditureReport
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import searData from '@/components/Search/mixin'

export default {
  name: 'OtherDetail',
  components: {
    selectCK: () => import('@/views/production/collect/components/seleceCK')
  },
  mixins: [searData],
  data() {
    return {
      seleceVisiblit: false,
      fdate: '',
      workShopName: '',
      stockname: '',
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.fmaterial'), type: 'tag', prop: 'fnumber', width: '120' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname', width: '120' },
        { label: this.$t('shop.fspecificaTion'), prop: 'FSPECIFICATION', width: '160' },
        { label: this.$t('shop.fdescripTion'), prop: 'fdescription', width: '200' },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.stock2'), prop: 'erpstock', width: '150' },
        { label: this.$t('market.fdate'), prop: 'fdate', width: '120' },
        { label: this.$t('market.resultqty1'), prop: 'resultqty', width: '90' },
        { label: this.$t('market.materQty'), prop: 'fqty', width: '140' },
        { label: this.$t('market.statusSz'), prop: 'status', width: '90' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryRevenueAndExpenditureReport({
            date: this.fdate,
            fnumber: this.workShopName,
            fstockId: this.fstockId,
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '存货收发数据校验报表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取财务报表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        fdate: this.fdate,
        fnumber: this.workShopName,
        fstockId: this.fstockId,
        pageNum: this.currentPage,
        pageSize: this.size
        // search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await queryRevenueAndExpenditureReport(DATA)
      this.tableData = RES.list
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 仓库选中
    selectCK(ev) {
      if (ev.fstockId) {
        this.fstockId = ev.fstockId
        this.stockname = ev.fname
      }
      this.seleceVisiblit = false
    },
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev.fnumber}` })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  padding: 10px;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 14px;
        text-align: center;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
